package com.xinz.serialization;

import java.lang.reflect.Field;

public class FieldAccessor implements Accessor{
	
	Field field;
	
	Object target;
	
	public FieldAccessor(){
		
	}
	
	public Field getField(){
		return field;
	}
	
	public Object getTarget() {
		return target;
	}
	
	public void setTarget(Field field,Object target) {
		this.field = field;
		this.target = target;
	}
	
	@Override
	public int getInt() throws  IllegalAccessException {
		if(field.getType()==int.class){
			return field.getInt(target);
		}else {
			Integer result = (Integer) field.get(target);
			return result==null?0:result;
		}
		
	}
	
	@Override
	public void setInt(int value) throws IllegalAccessException {
		field.setInt(target,value);
	}
	
	@Override
	public float getFloat() throws  IllegalAccessException {
		return field.getFloat(target);
	}
	
	@Override
	public void setFloat(float value) throws IllegalAccessException {
		field.setFloat(target,value);
	}
	
	@Override
	public Object get() throws IllegalAccessException {
		return field.get(target);
	}
	
	@Override
	public void set(Object value) throws  IllegalAccessException {
		field.set(target, value);
	}
	
	@Override
	public boolean getBoolean() throws  IllegalAccessException {
		return field.getBoolean(target);
	}
	
	@Override
	public void setBoolean(boolean value) throws  IllegalAccessException  {
		field.setBoolean(target, value);
	}
	
	@Override
	public void setLong(long value)  throws  IllegalAccessException {
		field.setLong(target,value);
	}
	
	@Override
	public long getLong() throws  IllegalAccessException  {
		return field.getLong(target);
	}
	
	@Override
	public String getString() throws IllegalAccessException {
		return (String)get();
	}
	
	@Override
	public void setString(String value) throws IllegalAccessException {
		set(value);
	}
	
	@Override
	public Class<?> getType() throws IllegalAccessException{
		if(field.getType().isPrimitive()){
			return field.getType();
		}else{
			return field.get(target).getClass();
		}
	}
	
	@Override
	public void setShort(short value) throws IllegalArgumentException, IllegalAccessException {
		field.setShort(target, value);		
	}
	
	@Override
	public short getShort() throws IllegalArgumentException, IllegalAccessException {
		return field.getShort(target);
	}
	
	@Override
	public double getDouble() throws IllegalArgumentException, IllegalAccessException {
		return field.getDouble(target);
	}
	
	@Override
	public void setDouble(double value) throws IllegalArgumentException, IllegalAccessException {
		field.setDouble(target, value);
		
	}
	
	@Override
	public int getChar() throws IllegalArgumentException, IllegalAccessException {
		return field.getChar(target);
	}
	
	@Override
	public void setChar(char value) throws IllegalArgumentException, IllegalAccessException {
		field.setChar(target, value);
	}
	
	@Override
	public void setByte(byte value) throws IllegalAccessException {
		field.setByte(target, value);
	}
	
	@Override
	public byte getByte() throws IllegalArgumentException, IllegalAccessException {
		return field.getByte(target);
	}
	
	@Override
	public boolean isNull() throws IllegalArgumentException, IllegalAccessException {
		if(field.getType().isPrimitive()){
			return false;
		}else{
			try{
				return field.get(target)==null;
			}catch (IllegalArgumentException e) {
				throw e;
			}
		}
	}
}
